#!/usr/bin/env python
# _*_ coding: utf-8 _*_
"""
author : Administrator
project : ApiTestPlatform
date : 2022/2/8
"""

import csv
import os

basedir = os.path.abspath(os.path.dirname(__file__))
# testfile = basedir + '/users.csv'


def read_cvs(filepath):
    with open(filepath, 'r', encoding='utf8') as fp:  # 读取文件
        # 使用列表推导式，将数据装进列表
        data_list = [i for i in csv.reader(fp)]
    return data_list

def write_cvs(filepath,old_data,head,value):
    """ head = ["编号", "课程", "讲师"]
        value:
        @param filepath: 文件路径
        @param old_data: 旧数据
        @param head:  例如：["测试域名","正式域名","报告路径"]
        @param value: 新增的值
    """
    new_data = old_data.append(value)
    try:
        with open(filepath,'w',encoding='utf8',newline='') as csv_file:
            writer = csv.writer(csv_file)  # dialect='excel' 指定格式
            if head is not None:
                writer.writerow(head)
            for row in new_data:
                writer.writerow(row)
            print("写入成功！")
    except:
        print("写入文件出错！")


def parse_csv():
    ''' 解析charles导出的csv文件，批量形成测试脚本'''
    # Todo:
    # 1 截掉URL中 host
    # 2.获取Method
    # 3.Content-Type  不知是否必要
    # 4.创建方法，取后2字段color/config，成 def color_config()

    # writer.writerow(newdata_list)
# data = {('测试1','123'),
# ('测试2','12312')}
#
# f2= open(testfile,'w',encoding='utf8')
# writer = csv.writer(f2)
# for i in data:
#     writer.writerow(i)
# f2.close()
